<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<!-- no <xsl:output> here, control it in each theme -->

	<xsl:include href="xsl-utilities.xsl"/>
	<xsl:include href="Forms.xsl"/>
	<xsl:include href="i18n.xsl"/>

	<!-- info about this website -->
	<xsl:variable name="Site" select="//gxpage-runtime/Site"/>
	<!-- info about the layout of directories -->
	<xsl:variable name="Layout" select="//gxpage-runtime/Layout"/>
	<!-- info about this run -->
	<xsl:variable name="Run" select="//gxpage-runtime/Run"/>
	<!-- site-wide parameters and values -->
	<xsl:variable name="Params" select="$Run/Params"/>
	<!-- submitted name/value pairs -->
	<xsl:variable name="Request" select="//gxpage-runtime/Run/Request"/>
	<!-- theme node -->
	<xsl:variable name="Theme" select="//gxpage-runtime/Theme"/>
	<!-- full URI without Self -->
	<xsl:variable name="Root" select="$Layout/Client/Root"/>
	<!-- php file itself -->
	<xsl:variable name="Self" select="concat($Root,$Layout/Client/Self)"/>
	<!-- base path of the javascript dir -->
	<xsl:variable name="Scripts" select="concat($Root,$Layout/Client/JavaScript)"/>
	<!-- base href -->
	<xsl:variable name="BaseHREF" select="concat($Layout/Client/BaseHREF,$Theme/BasePath)"/>

	<!-- top-level variables -->
	<xsl:variable name="Page" select="$Params/Page"/>
	<xsl:variable name="Action" select="$Params/Action"/>
	<xsl:variable name="Language" select="$Params/Lang"/>
	<xsl:variable name="PrimaryLanguage" select="substring-before($Language,'-')"/>

	<!-- current user logged in -->
	<xsl:variable name="User" select="//gxpage-runtime/Site/User"/>

	<!-- page vars for use in javascript functions -->
	<xsl:variable name="ScriptVars">
		var GXPage = new Object();
		GXPage.Theme	= '<xsl:value-of select="$Theme/BasePath"/>';
		GXPage.Scripts	= '<xsl:value-of select="$Scripts"/>';
		GXPage.Host		= '<xsl:value-of select="$Run/Host"/>';
		GXPage.Root		= '<xsl:value-of select="$Root"/>';
		GXPage.Self		= '<xsl:value-of select="$Self"/>';
		GXPage.Page		= '<xsl:value-of select="$Page"/>';
		GXPage.Action	= '<xsl:value-of select="$Action"/>';
		GXPage.User		= '<xsl:value-of select="normalize-space(concat($User/FirstName,' ',$User/LastName))"/>';
		GXPage.Absolute	= GXPage.Host + ( (GXPage.Root == '/') ? '' : GXPage.Root ) + GXPage.Self;
		GXPage.go		= function( page, protocol ){
			if( page ){
				pre = protocol || '';
				url = (pre) ? pre + '://' + GXPage.Absolute : GXPage.Self;
				document.location.href = url + '/' + page;
			}
		}
	</xsl:variable>

	<xsl:template name="showHead">
		<title><xsl:value-of select="concat($Site/Info/Name,' - ',//Page[ @ID = $Page ]/Title)"/></title>

		<!-- set the base to the root level of the selected theme -->
		<base href="{$BaseHREF}"></base>

		<!-- language and encoding -->
		<meta http-equiv="Content-Language" content="{$Language}" />
		<meta http-equiv="Content-Type" content="text/html; charset={$Run/Encoding}" />

		<!-- tell search engines to ignore these pages if logged in -->
		<xsl:if test="$User/*">
			<meta name="ROBOTS" content="NOINDEX,NOFOLLOW" />
			<meta name="GOOGLEBOT" content="NOINDEX,NOFOLLOW" />
		</xsl:if>

		<!-- include the script vars -->
		<script type="text/javascript">
			<xsl:value-of select="$ScriptVars"/>
		</script>
	</xsl:template>

	<xsl:template match="Content">
		<!-- the xpath to this content node -->
		<xsl:variable name="xpath">
			<xsl:for-each select="ancestor-or-self::*">
				<xsl:if test="position() = 1">
					<xsl:text>/</xsl:text>
				</xsl:if>
				<xsl:choose>
					<xsl:when test="name() = 'Content'">
						<xsl:value-of select="concat(name(),'[',count(preceding-sibling::Content)+1,']')"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="concat(name(),'[',count(preceding-sibling::*)+1,']')"/>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:if test="not( position() = last() )">
					<xsl:text>/</xsl:text>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		<!-- this gets the position of this occurrence of this node in the entire document -->
		<xsl:variable name="nodePosition">
			<xsl:number level="any" count="Content" format="1"/>
		</xsl:variable>
		<!-- give it a unique name -->
		<xsl:variable name="name" select="concat('content',$nodePosition)"/>
		<div class="GXEditorContent" style="position: relative;">
			<xsl:if test="$User">
				<xsl:attribute name="alt">
					<xsl:value-of select="$xpath"/>
				</xsl:attribute>
				<xsl:attribute name="id">
					<xsl:value-of select="concat('Content',$nodePosition)"/>
				</xsl:attribute>
			</xsl:if>
			<!-- now grab the value ... if there are child nodes, use copy-of -->
			<xsl:choose>
				<xsl:when test="count(*)">
					<xsl:copy-of select="."/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="." disable-output-escaping="yes"/>
				</xsl:otherwise>
			</xsl:choose>
		</div>
	</xsl:template>

	<xsl:template match="Picture">
		<xsl:if test="string-length(@Name) &gt; 0">
			<!-- the xpath to this content node -->
			<xsl:variable name="xpath">
				<xsl:for-each select="ancestor-or-self::*">
					<xsl:if test="position() = 1">
						<xsl:text>/</xsl:text>
					</xsl:if>
					<xsl:choose>
						<xsl:when test="name() = 'Picture'">
							<xsl:value-of select="concat(name(),'[',count(preceding-sibling::Picture)+1,']')"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="concat(name(),'[',count(preceding-sibling::*)+1,']')"/>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:if test="not( position() = last() )">
						<xsl:text>/</xsl:text>
					</xsl:if>
				</xsl:for-each>
			</xsl:variable>
			<!-- this gets the position of this occurrence of this node in the entire document -->
			<xsl:variable name="nodePosition">
				<xsl:number level="any" count="Picture" format="1"/>
			</xsl:variable>
			<!-- give it a unique name -->
			<xsl:variable name="name" select="concat('picture',$nodePosition)"/>
			<p class="GXEditorPicture">
				<xsl:if test="$User">
					<xsl:attribute name="id">
						<xsl:value-of select="$xpath"/>
					</xsl:attribute>
				</xsl:if>
				<img src="{$Layout/Client/SharedGraphics}{@Name}"/>
			</p>
		</xsl:if>
	</xsl:template>

	<xsl:template name="showMessageList">
		<xsl:variable name="errorList" select="/Page/gxpage-runtime/Messages/*[ type = 'UserNotice' ]"/>
		<xsl:if test="count($errorList) &gt; 0">
			<ul id="Messages">
				<xsl:for-each select="$errorList">
					<li><xsl:value-of select="msg"/></li>
				</xsl:for-each>
			</ul>
		</xsl:if>
	</xsl:template>

	<xsl:template name="showErrorList">
		<xsl:variable name="errorList" select="/Page/gxpage-runtime/Messages/*[ not(type = 'UserNotice') ]"/>
		<xsl:if test="count($errorList) &gt; 0">
			<table border="1">
				<thead>
					<tr>
					<td>Index</td>
					<td>Severity</td>
					<td>Msg</td>
					<td>Debug</td>
					</tr>
				</thead>
				<tbody>
					<xsl:for-each select="$errorList">
						<xsl:sort select="index" data-type="number"/>
						<tr>
							<td><xsl:value-of select="index"/></td>
							<td><xsl:value-of select="type"/></td>
							<td><xsl:value-of select="msg"/></td>
							<td><xsl:value-of select="file"/>::<xsl:value-of select="line"/></td>
						</tr>
					</xsl:for-each>
				</tbody>
			</table>
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>
